/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdAdministrarUsuarios.java
 *
 * Created on 16/08/2010, 04:57:01 AM
 */

package gestionseguridadadministrativacliente;

import administracion.delegate.GestionSeguridadDelegate;
import administracion.helper.Utiles_MensajesDialogo;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.ELProperty;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;
import seguridad.administracion.ejb30.TO.UsuarioTO;

/**
 *
 * @author Administrador
 */
public class jdAdministrarUsuarios extends javax.swing.JDialog {

     private List<UsuarioTO> usuarios;
     private List<UsuarioTO> usuarios_temp;
     private JTableBinding jTableBinding;
     private BindingGroup bindingGroup;
     private jdAsignaSistemas asigna_sistemas;
     private jdAsignaDatos asigna_datos;
    /** Creates new form jdAdministrarUsuarios */
    public jdAdministrarUsuarios(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        try {
            this.usuarios = GestionSeguridadDelegate.getInstance().getListaUsuarios();
            this.usuarios_temp = this.usuarios;
            this.actualizaTablaUsuarios(this.usuarios_temp);
        } catch (Exception ex) {
            Logger.getLogger(jdAdministrarUsuarios.class.getName()).log(Level.SEVERE, null, ex);
        }
       
    }

     private void actualizaTablaUsuarios(List<UsuarioTO> users) {
            this.bindingGroup = new BindingGroup();
            this.jTableBinding = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ, users, this.jtUsuarios);

            JTableBinding.ColumnBinding columnBinding0 = this.jTableBinding.addColumnBinding(ELProperty.create("${dni}"));

            columnBinding0.setColumnName("DNI");
            columnBinding0.setColumnClass(String.class);
            columnBinding0.setEditable(false);

            JTableBinding.ColumnBinding columnBinding1= this.jTableBinding.addColumnBinding(ELProperty.create("${nombre}"));

            columnBinding1.setColumnName("USUARIO");
            columnBinding1.setColumnClass(String.class);
            columnBinding1.setEditable(false);


            /*JTableBinding.ColumnBinding columnBinding2 = this.jTableBinding.addColumnBinding(ELProperty.create("${cod_zona}"));

            columnBinding2.setColumnName("ZONA");
            columnBinding2.setColumnClass(String.class);
            columnBinding2.setEditable(false);

            JTableBinding.ColumnBinding columnBinding3 = this.jTableBinding.addColumnBinding(ELProperty.create("${unid_organizativa}"));

            columnBinding3.setColumnName("UNIDAD ORGANIZATIVA");
            columnBinding3.setColumnClass(String.class);
            columnBinding3.setEditable(false);*/

            this.bindingGroup.addBinding(this.jTableBinding);
            this.bindingGroup.bind();


        }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtUsuarios = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jbCancelar1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gestionseguridadadministrativacliente.GestionSeguridadAdministrativaclienteApp.class).getContext().getResourceMap(jdAdministrarUsuarios.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jtUsuarios.setAutoCreateRowSorter(true);
        jtUsuarios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "DNI", "Usuario"
            }
        ));
        jtUsuarios.setName("jtUsuarios"); // NOI18N
        jScrollPane1.setViewportView(jtUsuarios);

        jButton1.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/list_add.png"))); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/list_remove.png"))); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jbCancelar1.setFont(resourceMap.getFont("jbCancelar1.font")); // NOI18N
        jbCancelar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbCancelar1.setMnemonic('C');
        jbCancelar1.setText(resourceMap.getString("jbCancelar1.text")); // NOI18N
        jbCancelar1.setToolTipText(resourceMap.getString("jbCancelar1.toolTipText")); // NOI18N
        jbCancelar1.setName("jbCancelar1"); // NOI18N
        jbCancelar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelar1ActionPerformed(evt);
            }
        });
        jbCancelar1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbCancelar1KeyPressed(evt);
            }
        });

        jButton3.setFont(resourceMap.getFont("jButton3.font")); // NOI18N
        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/list_add.png"))); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 478, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton2)
                            .addComponent(jButton3)
                            .addComponent(jButton1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(272, 272, 272)
                        .addComponent(jbCancelar1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(195, 195, 195)
                        .addComponent(jLabel1)))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2, jButton3});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(66, 66, 66)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbCancelar1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
          String cod_pedido = "";
         int filaElegida = this.jtUsuarios.getSelectedRow();

         if (filaElegida < 0) {
              Utiles_MensajesDialogo.mensajeError("Debe elegir un Usuario!");
              this.jtUsuarios.requestFocus();
              return;
          }
         else{
             if (Utiles_MensajesDialogo.mensajeConfirmacion("Esta seguro de eliminar el Usuario?")){
                try {
                    UsuarioTO usuario = (UsuarioTO) this.usuarios_temp.get(this.jtUsuarios.convertRowIndexToModel(filaElegida));
                    if (GestionSeguridadDelegate.getInstance().eliminarUsuario(usuario.getDni())) {
                         this.usuarios_temp.remove(filaElegida);
                         this.actualizaTablaUsuarios(this.usuarios_temp);
                         Utiles_MensajesDialogo.mensajeExito();
                    }

                } catch (Exception ex) {
                    Utiles_MensajesDialogo.mensajeError("Nose pudo realizar la operacion");
                }

             }

             
         }


    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
      int filaElegida = this.jtUsuarios.getSelectedRow();

         if (filaElegida < 0) {
              Utiles_MensajesDialogo.mensajeError("Debe elegir un Usuario!");
              this.jtUsuarios.requestFocus();
              return;
          }
         else{
                UsuarioTO usuario = (UsuarioTO) this.usuarios_temp.get(this.jtUsuarios.convertRowIndexToModel(filaElegida));
                this.asigna_sistemas = new jdAsignaSistemas(null, true,usuario.getDni());
                this.asigna_sistemas.setLocationRelativeTo(null);
                this.asigna_sistemas.setVisible(true);
             }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jbCancelar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelar1ActionPerformed

        this.dispose();
}//GEN-LAST:event_jbCancelar1ActionPerformed

    private void jbCancelar1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbCancelar1KeyPressed
     
}//GEN-LAST:event_jbCancelar1KeyPressed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
         int filaElegida = this.jtUsuarios.getSelectedRow();

         if (filaElegida < 0) {
              Utiles_MensajesDialogo.mensajeError("Debe elegir un Usuario!");
              this.jtUsuarios.requestFocus();
              return;
          }
         else{
                 UsuarioTO usuario = (UsuarioTO) this.usuarios_temp.get(this.jtUsuarios.convertRowIndexToModel(filaElegida));
                this.asigna_datos = new jdAsignaDatos(null, true,usuario.getDni());
                this.asigna_datos.setLocationRelativeTo(null);
                this.asigna_datos.setVisible(true);
             }
    }//GEN-LAST:event_jButton3ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                jdAdministrarUsuarios dialog = new jdAdministrarUsuarios(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbCancelar1;
    private javax.swing.JTable jtUsuarios;
    // End of variables declaration//GEN-END:variables

}
